package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.commons.util.StringUtils;
import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysFeedback;
import cn.wgx.modules.sys.services.SysFeedbackService;
import cn.wgx.modules.sys.util.UserTypeUtil;
import cn.wgx.modules.sys.util.UserUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Api(tags = "6. 平台反馈")
@RestController
@RequestMapping(value = "${allPathPre}sysfeedback")
public class SysFeedbackController extends BaseController {

    @Resource
    SysFeedbackService sysFeedbackService;


    @ApiOperation(value = "获取全部反馈列表", notes = "只有平台管理者可以看到全部反馈信息,  非平台管理员只能看到本人发送的反馈信息.")
    @RequestMapping(path = "list", method = RequestMethod.GET)
    @ApiPermission
    public Map<String, Object> getList(@ModelAttribute SysFeedback sysFeedback,
                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        if(!UserTypeUtil.isAdmin()){
            sysFeedback.setCreateUserId(UserUtil.getUser().getUser_id());
        }
        return returnData(PageHelper.startPage(page,  Math.min(Math.abs(pageSize), maxPageSize)), sysFeedbackService.getList(sysFeedback));
    }


    @ApiOperation(value = "获取单个反馈信息")
    @ApiPermission
    @RequestMapping(path = "getOne", method = RequestMethod.GET)
    public Map<String, Object> getOne(@ApiParam("反馈id") @RequestParam Integer id) {
        return returnData(sysFeedbackService.getOne(id));
    }



    @ApiOperation(value = "发布反馈", notes = "必须参数: title, content. name会自动生成")
    @RequestMapping(path = "insert", method = RequestMethod.POST)
    @ApiPermission
    public Map<String, Object> inserOne(@RequestBody SysFeedback sysFeedback) {
        if(sysFeedback == null || StringUtils.isBlank(sysFeedback.getTitle())
                || StringUtils.isBlank(sysFeedback.getContent()) ){
            return returnErrorMap("缺少参数");
        }
        return returnData(sysFeedbackService.insertOne(sysFeedback));
    }


    @ApiOperation(value = "管理员回复反馈", notes = "必须参数: feedback_id, answer")
    @RequestMapping(path = "updateAnswer", method = RequestMethod.POST)
    @ApiPermission
    public Map<String, Object> update(@RequestBody SysFeedback sysFeedback) {
        if (sysFeedback == null || Validation.isNullOrZero(sysFeedback.getFeedback_id()) || StringUtils.isBlank(sysFeedback.getAnswer())) {
            return returnErrorMap("参数错误.");
        }
        return returnData(sysFeedbackService.update(sysFeedback.getFeedback_id(), sysFeedback.getAnswer()));
    }


    @ApiOperation(value = "设置已读", notes = "必须参数: feedback_id")
    @RequestMapping(path = "updateRead", method = RequestMethod.POST)
    @ApiPermission
    public Map<String, Object> read(@RequestBody SysFeedback sysFeedback) {
        if (sysFeedback == null || Validation.isNullOrZero(sysFeedback.getFeedback_id())) {
            return returnErrorMap("参数错误.");
        }
        return returnData(sysFeedbackService.update(sysFeedback.getFeedback_id()));
    }


    @ApiOperation(value = "删除某条反馈")
    @ApiPermission
    @RequestMapping(path = "del", method = RequestMethod.DELETE)
    public Map<String, Object> del(@ApiParam("反馈id") @RequestParam Integer id) {
        return returnData(sysFeedbackService.del(id));
    }

}
